<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Album extends GUARIPOLO_Controller
{

	public function index()
	{
		$this->load->library('facebook',array(
				'appId'  => '339581436114045',
				'secret' => 'f1e079e52e936aecbf44c3cf4784b4ad',
				'coockie'=> 'true',
		));
		
		$this->load->view('album',array('facebook'=>$this->facebook));

	}

	public function ajax1()
	{
		
		$this->load->library('facebook',array(
				'appId'  => '339581436114045',
				'secret' => 'f1e079e52e936aecbf44c3cf4784b4ad',
				'coockie'=> 'true',
		));
		$facebook=$this->facebook;
		$user = $facebook->getUser();
		
		if ($user)
		{
			try
			{
				$user_profile = $facebook->api('/me');
			}
			catch (FacebookApiException $e)
			{
				error_log($e);
				$user = null;
				echo "aaa**";
			}
		}
		
		if ($user)
		{
			$logoutUrl = $facebook->getLogoutUrl();
			$uid= $_POST['uid'] ;
		
			$params1=array( 'method' => 'fql.query','query' => "SELECT aid,name from album where owner = '".$uid."'" );
			$r1 = $facebook->api($params1);
		
			foreach($r1 as $aid)
			{
				$params=array( 'method' => 'fql.query','query' => "SELECT src_small,pid,src_big from photo where aid='".$aid['aid']."' LIMIT 6" );
				$r2=$facebook->api($params);
				echo "<h3>".$aid['name']." </h3>";
				foreach($r2 as $p)
				{
					echo "<img class='enlace' onclick=\"imageBig('".$p['src_big']."')\" src='".$p['src_small']."'/>";
				}
				$par="'".$uid."','".$aid['aid']."'";
				echo " <span class='enlace' onclick=\"albumComplete(".$par.")\">[ver mas]</span> <span class='enlace' onclick=\"descargarAlbum(".$par.")\">[descargar]</span><br>";
		
			}
			
		}
		else
		{
			$params = array('scope' => 'user_photos,friends_photos' );
			$loginUrl = $facebook->getLoginUrl($params);
		}
		
	}
	public function ajax2()
	{
		$this->load->library('facebook',array(
				'appId'  => '339581436114045',
				'secret' => 'f1e079e52e936aecbf44c3cf4784b4ad',
				'coockie'=> 'true',
		));
		$facebook=$this->facebook;
		$user = $facebook->getUser();
		
		if ($user)
		{
			try
			{
				$user_profile = $facebook->api('/me');
			}
			catch (FacebookApiException $e)
			{
				error_log($e);
				$user = null;
				echo "aaa**";
			}
		}
		
		if ($user)
		{
			$logoutUrl = $facebook->getLogoutUrl();
		
		
			function create_zip($files = array(),$destination = '',$overwrite = false) {
				if(file_exists($destination) && !$overwrite) {
					return -5;
				}
				$valid_files = array();
				if(is_array($files)) {
					foreach($files as $file) {
						if(file_exists($file)) {
							$valid_files[] = $file;
						}
					}
				}
				if(count($valid_files)) {
					$zip = new ZipArchive();
					if($zip->open($destination,$overwrite ? ZIPARCHIVE::OVERWRITE : ZIPARCHIVE::CREATE) !== true) {
						return false;
					}
					foreach($valid_files as $file) {
						$zip->addFile($file,$file);
					}
					$zip->close();
					return file_exists($destination);
				}
				else
				{
					return false;
				}
			}
		
		
		
			$uid= $_POST['uid'] ;
			$aid= $_POST['aid'];
		
			$params=array( 'method' => 'fql.query','query' => "SELECT pid,src_big from photo where aid='".$aid."'");
			$r2=$facebook->api($params);
		
			$ar=array();
			foreach($r2 as $p)
			{
				//	echo "<img src='".$p['src_small']."'/>";
				if(!is_dir('./user/'.$uid.'/img/'.$aid.'/'))
				{
					mkdir('./user/'.$uid.'/img/'.$aid.'/', 0777, true);
			
				}
				if(!file_exists('./user/'.$uid.'/img/'.$aid.'/'.$p["pid"].'.jpg'))
				{
					file_put_contents('./user/'.$uid.'/img/'.$aid.'/'.$p["pid"].'.jpg', file_get_contents($p['src_big']));
				}
				array_push($ar,'./user/'.$uid.'/img/'.$aid.'/'.$p["pid"].'.jpg');
					
			}
			$result=create_zip($ar,'./user/'.$uid.'/img/'.$aid.'/'.$aid.'.zip');
			if($result==true)
			{
				echo '<h2>COMPLETE! <a href="./user/'.$uid.'/img/'.$aid.'/'.$aid.'.zip'.'">[CLICK PARA DESCARGAR]</a> </h2>';
				echo '<br>Link: '.base_url().'user/'.$uid.'/img/'.$aid.'/'.$aid.'.zip';
			}
			else if($result==-5)
			{
				echo '<h2>COMPLETE! <a href="./user/'.$uid.'/img/'.$aid.'/'.$aid.'.zip'.'">[CLICK PARA DESCARGAR]</a> </h2>';
				echo '<br>Link: '.base_url().'user/'.$uid.'/img/'.$aid.'/'.$aid.'.zip';
			}
			else
			{
				echo "ERROR";
			}
		
		}
		else
		{
			$params = array('scope' => 'user_photos,friends_photos' );
			$loginUrl = $facebook->getLoginUrl($params);
		}
	}
	public function ajax3()
	{
		$this->load->library('facebook',array(
				'appId'  => '339581436114045',
				'secret' => 'f1e079e52e936aecbf44c3cf4784b4ad',
				'coockie'=> 'true',
		));
		$facebook=$this->facebook;
		$user = $facebook->getUser();
		
		if ($user)
		{
			try
			{
				$user_profile = $facebook->api('/me');
			}
			catch (FacebookApiException $e)
			{
				error_log($e);
				$user = null;
			}
		}
		
		if ($user)
		{
			$logoutUrl = $facebook->getLogoutUrl();
		
		
			$uid= $_POST['uid'] ;
			$aid= $_POST['aid'];
		
			$params=array( 'method' => 'fql.query','query' => "SELECT pid,src_small,src_big from photo where aid='".$aid."'");
			$r2=$facebook->api($params);
			$par="'".$uid."','".$aid."'";
			echo "<span  onclick=\"descargarAlbum(".$par.")\"><h1 class='enlace'>[descargar]</h1></span><br>";
		
			foreach($r2 as $p)
			{
				echo "<img class='enlace' onclick=\"imageBig('".$p['src_big']."')\" src='".$p['src_small']."'/>";
			}
		
			
		
		
		}
		else
		{
			$params = array('scope' => 'user_photos,friends_photos' );
			$loginUrl = $facebook->getLoginUrl($params);
		}
	}

}
?>